﻿Imports System.IO
Imports PilotKaraoke.ApplicationFramework.ApplicationTips

Public Class MainForm

#Region " Fields "

    Private _Data As TipsDataSet
    Private _OpenFileName As String = String.Empty

#End Region

#Region " Methods "

#Region " Configuration Methods "

    ''' <summary>
    ''' Sets the form configuration.
    ''' </summary>
    ''' <remarks>Intended only for 1 use at the time the form is first displayed.</remarks>
    Private Sub ConfigureForm()
        Me.Icon = My.Resources.ApplicationIcon
    End Sub

    ''' <summary>
    ''' Configures the form title to include the name of the open file if applicable.
    ''' </summary>
    ''' <remarks>Execute this method any time a file is opened, closed, or saved.</remarks>
    Private Sub ConfigureFormTitle()
        If Not String.IsNullOrEmpty(_OpenFileName) Then
            Me.Text = String.Format("{0} - {1}", My.Application.Info.ProductName, Path.GetFileName(_OpenFileName))
        Else
            Me.Text = My.Application.Info.ProductName
        End If
    End Sub

    ''' <summary>
    ''' Sets the binding source to the underlying data source.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetDataSources()
        Me.TipsBindingSource.DataSource = _Data
    End Sub

    ''' <summary>
    ''' Configures the data entry controls based on the current state of the underlying data source.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ConfigureDataEntryControls()
        If _Data Is Nothing OrElse Me.TipsBindingSource.Count = 0 Then
            DisableDataEntryControls()
        Else
            EnableDataEntryControls()
        End If
    End Sub

    ''' <summary>
    ''' Disables the data entry controls.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DisableDataEntryControls()
        Me.TipCaptionTextBox.Enabled = False
        Me.TipContentRichTextBox.Enabled = False
    End Sub

    ''' <summary>
    ''' Enables the data entry controls.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EnableDataEntryControls()
        Me.TipCaptionTextBox.Enabled = True
        Me.TipContentRichTextBox.Enabled = True
    End Sub

    ''' <summary>
    ''' Confgiures the controls that change when the OrderTabPage becomes active
    ''' </summary>
    ''' <remarks>This page is intended only for setting the display order and not for editing tip content.</remarks>
    Private Sub ConfigureOrderTabPage()

        Me.BindingNavigatorAddNewItem.Enabled = False
        Me.BoldToolStripButton.Enabled = False
        Me.ItalicToolStripButton.Enabled = False

    End Sub

    ''' <summary>
    ''' Confgiures the controls that change when the EditTabPage becomes active
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ConfigureEditTabPage()

        Me.BindingNavigatorAddNewItem.Enabled = True
        Me.BoldToolStripButton.Enabled = True
        Me.ItalicToolStripButton.Enabled = True

    End Sub

#End Region

#Region " File Methods "

    ''' <summary>
    ''' Points the underlying data source to a new instance of the Application Tip data.
    ''' </summary>
    ''' <returns>Boolean - indicating if the operation succeeded.</returns>
    ''' <remarks></remarks>
    Private Function FileNew() As Boolean
        If FileClose() Then
            _Data = New TipsDataSet()
            SetDataSources()
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' Closes the currently open file if one is open.
    ''' </summary>
    ''' <returns>Boolean - indicating if the operation succeeded.</returns>
    ''' <remarks>If the underlying data source has changes the user will be prompted to confirm.</remarks>
    Private Function FileClose() As Boolean
        If _Data Is Nothing Then
            _OpenFileName = String.Empty
            ConfigureFormTitle()
            Return True
        Else
            If _Data.HasChanges Then
                Dim message As String = "The open data file has unsaved changes.  Changes will be lost."
                If MessageBox.Show(message, "Are you Sure?", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
                    _Data = Nothing
                    _OpenFileName = String.Empty
                    ConfigureFormTitle()
                    Return True
                Else
                    Return False
                End If
            Else
                _Data = Nothing
                _OpenFileName = String.Empty
                ConfigureFormTitle()
                Return True
            End If
        End If
    End Function

    ''' <summary>
    ''' Shows the open file dialog and opens the user selected Applicaiton Tips XML file.
    ''' </summary>
    ''' <returns>Boolean - indicating if the operation succeeded.</returns>
    ''' <remarks></remarks>
    Private Function FileOpen() As Boolean
        If Me.OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Return FileOpen(Me.OpenFileDialog.FileName)
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' Opens the specified file.
    ''' </summary>
    ''' <param name="filename">String - the file to open.</param>
    ''' <returns>Boolean - indicating if the operation succeeded.</returns>
    ''' <remarks>If the filename is not a valid XML Tips file, an unhandled excpetion will occur.</remarks>
    Private Function FileOpen(ByVal filename As String) As Boolean
        _Data = New TipsDataSet(filename)
        _OpenFileName = filename
        ConfigureFormTitle()
        SetDataSources()
        Return True
    End Function

    ''' <summary>
    ''' Shows the save file dialog and saves the current file to the user selected file name.
    ''' </summary>
    ''' <returns>Boolean - indicating if the operation succeeded.</returns>
    ''' <remarks></remarks>
    Private Function FileSave() As Boolean
        If Me.SaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Return FileSave(Me.SaveFileDialog.FileName)
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' Saves the current file to the filename specified.
    ''' </summary>
    ''' <param name="filename">String - the file name and path to save the data to.</param>
    ''' <returns>Boolean - indicating if the operation succeeded.</returns>
    ''' <remarks></remarks>
    Private Function FileSave(ByVal filename As String) As Boolean
        If String.IsNullOrEmpty(filename) Then
            Return FileSave()
        Else
            Me.TipsBindingSource.EndEdit()
            _Data.SaveToDisc(filename)
            _OpenFileName = filename
            ConfigureFormTitle()
            Return True
        End If
    End Function

#End Region

#Region " Status Message Methods "

    ''' <summary>
    ''' Writes a status message to the status bar with the forecolor of Black.
    ''' </summary>
    ''' <param name="message">String - the message to write.</param>
    ''' <remarks></remarks>
    Public Sub WriteStatusMessage(ByVal message As String)
        WriteStatusMessage(message, Color.Black)
    End Sub

    ''' <summary>
    ''' Write a status message to the status bar in the sepcified forecolor.
    ''' </summary>
    ''' <param name="message">String - the message to write.</param>
    ''' <param name="forecolor">Color - the color to use for the forecolor of the status message.</param>
    ''' <remarks></remarks>
    Public Sub WriteStatusMessage(ByVal message As String, ByVal forecolor As Color)
        Me.StatusMessageTimer.Stop()
        Me.MainToolStripStatusLabel.Text = message
        Me.MainToolStripStatusLabel.ForeColor = forecolor
        Me.StatusMessageTimer.Enabled = True
        Me.StatusMessageTimer.Start()
    End Sub

#End Region

#Region " Other Methods "

#End Region

#End Region

#Region " Event Handlers "

#Region " Form Events "

    Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Not FileClose() Then
            e.Cancel = True
        End If
    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ConfigureForm()

        If My.Application.CommandLineArgs.Count > 0 Then
            Dim commandLineFileName As String = My.Application.CommandLineArgs(0)
            FileOpen(commandLineFileName)
        Else
            FileNew()
        End If
    End Sub

    Private Sub MainForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        WriteStatusMessage(String.Format("Welcome to {0}!", My.Application.Info.ProductName), Color.DarkBlue)
        BringToFront()
        Utilities.ShowTipsAtStartup()
    End Sub

#End Region

#Region " Toolbar Events "

    Private Sub FileNewToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileNewToolStripButton.Click
        If FileNew() Then
            WriteStatusMessage("New file created.")
        End If
    End Sub

    Private Sub FileOpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileOpenToolStripButton.Click
        If FileOpen() Then
            WriteStatusMessage(String.Format("{0} opened.", Path.GetFileName(_OpenFileName)), Color.Green)
        End If
    End Sub

    Private Sub FileSaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileSaveToolStripButton.Click
        If FileSave(_OpenFileName) Then
            WriteStatusMessage(String.Format("{0} saved.", Path.GetFileName(_OpenFileName)), Color.Green)
        End If
    End Sub

    Private Sub FileSaveAsToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileSaveAsToolStripButton.Click
        If FileSave() Then
            WriteStatusMessage(String.Format("{0} saved.", Path.GetFileName(_OpenFileName)), Color.Green)
        End If
    End Sub

    Private Sub BoldToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BoldToolStripButton.Click

        'Xor operator is used to allow for untoggling of bold if it is already selected.
        Dim newStyle As FontStyle = Utilities.GetFontStyle(Me.TipContentRichTextBox.SelectionFont) Xor FontStyle.Bold
        Me.TipContentRichTextBox.SelectionFont = New Font(Me.TipContentRichTextBox.SelectionFont, newStyle)

    End Sub

    Private Sub ItalicToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ItalicToolStripButton.Click

        Dim newStyle As FontStyle = Utilities.GetFontStyle(Me.TipContentRichTextBox.SelectionFont) Xor FontStyle.Italic
        Me.TipContentRichTextBox.SelectionFont = New Font(Me.TipContentRichTextBox.SelectionFont, newStyle)

    End Sub

    Private Sub ResetDisplayDatesToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetDisplayDatesToolStripButton.Click
        If _Data IsNot Nothing Then
            If _Data.Tips.ResetDisplayedDates() Then
                WriteStatusMessage("Displayed dates successfully reset.", Color.Green)
            End If
        End If
    End Sub

    Private Sub SettingsToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SettingsToolStripButton.Click
        Using frm As New SettingsForm()
            frm.ShowDialog()
        End Using
    End Sub

#End Region

#Region " Binding Source Events "

    Private Sub TipsBindingSource_ListChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles TipsBindingSource.ListChanged
        ConfigureDataEntryControls()
    End Sub

#End Region

#Region " Other Event Handlers "

    Private Sub MainTabControl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MainTabControl.SelectedIndexChanged
        Select Case Me.MainTabControl.SelectedTab.Name
            Case "EditTipsTabPage"
                ConfigureEditTabPage()
            Case "EditOrderTabPage"
                ConfigureOrderTabPage()
        End Select
    End Sub

    Private Sub StatusMessageTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusMessageTimer.Tick
        Dim thisTimer As Timer = CType(sender, Timer)
        thisTimer.Stop()
        thisTimer.Enabled = False
        Me.MainToolStripStatusLabel.Text = String.Empty
        Me.MainToolStripStatusLabel.ForeColor = Color.Black
    End Sub

#End Region

#End Region



End Class
